package com.zhuoya.healthcard.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

import com.zhuoya.healthcard.contants.Contants;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import static com.zhuoya.healthcard.contants.Contants.HANDLEDETAILS1;
import static com.zhuoya.healthcard.contants.Contants.HANDLEDETAILS2;
import static com.zhuoya.healthcard.contants.Contants.HANDLEDETAILS3;
import static com.zhuoya.healthcard.contants.Contants.HANDLEDETAILS4;
import static com.zhuoya.healthcard.contants.Contants.HANDLEDETAILS5;
import static com.zhuoya.healthcard.contants.Contants.HANDLEDETAILS6;
import static com.zhuoya.healthcard.contants.Contants.HANDLEDETAILS7;
import static com.zhuoya.healthcard.contants.Contants.HOSPITALADDRESS;
import static com.zhuoya.healthcard.contants.Contants.HOSPITALID;
import static com.zhuoya.healthcard.contants.Contants.HOSPITALNAME;
import static com.zhuoya.healthcard.contants.Contants.HOSPITALPHONE;

public class ImageUtil {
    private static final int BLACK = -16777216;
    private static final int WHITE = -1;
    private static BarcodeFormat barcodeFormat;

    static {
        ImageUtil.barcodeFormat = BarcodeFormat.CODE_128;
    }

    public static Bitmap addTextA4(final Bitmap bitmap, final String s, final String s2, final String s3, final String s4, final String s5, final String s6, final String s7) throws Exception {
        final Bitmap decodeStream = BitmapFactory.decodeStream((InputStream) new FileInputStream("/mnt/internal_sd/Download/RegistrationForm.png"));
        final Bitmap bitmap2 = Bitmap.createBitmap(decodeStream.getWidth(), decodeStream.getHeight(), Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap2);
        canvas.drawBitmap(decodeStream, 0.0f, 0.0f, (Paint) null);
        canvas.drawBitmap(creatBarcode(s, 500, 80), 260.0f, 375.0f, (Paint) null);
        canvas.drawBitmap(imageScale(bitmap, 250, 300), 1855.0f, 365.0f, (Paint) null);
        final Paint paint = new Paint();
        paint.setTextSize(38.0f);
        canvas.drawText(s, 380.0f, 500.0f, paint);
        canvas.drawText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()), 400.0f, 630.0f, paint);
        canvas.drawText(s3, 1175.0f, 630.0f, paint);
        canvas.drawText(s2, 320.0f, 710.0f, paint);
        canvas.drawText(s4, 630.0f, 710.0f, paint);
        canvas.drawText(s5, 915.0f, 710.0f, paint);
        canvas.drawText(s6, 1190.0f, 710.0f, paint);
        if (!"".equals(s7)) {
            canvas.drawText(s7, 1485.0f, 710.0f, paint);
        }
        canvas.save();
        canvas.restore();
        saveBitmap(bitmap2, "/mnt/internal_sd/Download/", "tempRegistration.jpg");
        return bitmap2;
    }

    public static Bitmap addTextA5(String s, String string, final String s2, final String s3, final String s4, final String s6) throws Exception {
        final Bitmap decodeStream = BitmapFactory.decodeStream((InputStream) new FileInputStream("/mnt/internal_sd/Download/registration.png"));
        final Bitmap bitmap = Bitmap.createBitmap(decodeStream.getWidth(), decodeStream.getHeight(), Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(decodeStream, 0.0f, 0.0f, (Paint) null);
        canvas.drawBitmap(creatBarcode(s3, 400, 80), 720.0f, 94.0f, (Paint) null);
        final Paint paint = new Paint();
        paint.setTextSize(24.0f);
        if (s4 != null) {
            canvas.drawText(s4, 338.0f, 263.0f, paint);
        }

        final int length = s.length();
        if (length == 2) {
            canvas.drawText(s, 338.0f, 334.0f, paint);
        } else if (length == 3) {
            canvas.drawText(s, 322.0f, 334.0f, paint);
        } else if (length == 4) {
            canvas.drawText(s, 309.0f, 334.0f, paint);
        } else {
            canvas.drawText(s, 302.0f, 334.0f, paint);
        }
        canvas.drawText(string, 720.0f, 334.0f, paint);
        canvas.drawText(s2, 1090.0f, 334.0f, paint);


/*        canvas.drawText(HANDLEDETAILS1, 100.0f, 400.0f, paint);
        canvas.drawText(HANDLEDETAILS2, 100.0f, 430.0f, paint);
        final StringBuilder sb = new StringBuilder();
        sb.append(HANDLEDETAILS3);
        sb.append(Contants.HOSPITALWEEK);
        sb.append(" ");
        string = (s = sb.toString());
        if (StringUtils.isNotEmpty(Contants.HOSPITALAM)) {
            final StringBuilder sb2 = new StringBuilder();
            sb2.append(string);
            sb2.append(Contants.HOSPITALAM);
            sb2.append(" ");
            s = sb2.toString();
        }
        string = s;
        if (StringUtils.isNotEmpty(Contants.HOSPITALPM)) {
            final StringBuilder sb3 = new StringBuilder();
            sb3.append(s);
            sb3.append(Contants.HOSPITALPM);
            string = sb3.toString();
        }
        final StringBuilder sb4 = new StringBuilder();
        sb4.append(string);
        sb4.append("法定假日除外。");
        canvas.drawText(sb4.toString(), 100.0f, 460.0f, paint);
        canvas.drawText(HANDLEDETAILS4, 100.0f, 490.0f, paint);
        canvas.drawText(HANDLEDETAILS5, 100.0f, 520.0f, paint);
        if (HOSPITALID == 112) { // 佛祖岭
            canvas.drawText("6、体检流程：①一楼“放射科“透视→②二楼面对“体检登记处“左边检查室（1）", 100.0f, 550.0f, paint);
            canvas.drawText("           做皮肤痢疾→③面对前台最左边化验室做甲肝、戊肝", 100.0f, 580.0f, paint);
            canvas.drawText(HANDLEDETAILS7 + s6, 100.0f, 610.0f, paint);
        } else {
            canvas.drawText(HANDLEDETAILS6, 100.0f, 550.0f, paint);
            canvas.drawText(HANDLEDETAILS7 + s6, 100.0f, 580.0f, paint);
        }*/
        if (HOSPITALID == 119) {
            canvasDrawText119(canvas, paint);
        } else {
            canvasDrawText(canvas, s, string, paint, s6);
        }


        canvas.drawText(HOSPITALNAME, 118.0f, 658.0f, paint);
        final StringBuilder sb5 = new StringBuilder();
        sb5.append("电话：");
        sb5.append(HOSPITALPHONE);
        canvas.drawText(sb5.toString(), 118.0f, 698.0f, paint);
        final StringBuilder sb6 = new StringBuilder();
        sb6.append("地址：");
        sb6.append(HOSPITALADDRESS);
        canvas.drawText(sb6.toString(), 118.0f, 738.0f, paint);
        final Paint paint2 = new Paint();
        paint2.setTextSize(30.0f);
        canvas.drawText(s3.replace("", "  ").trim(), 788.0f, 204.0f, paint2);
        canvas.save();
        canvas.restore();
        return bitmap;
    }

    public static void canvasDrawText119(Canvas canvas, Paint paint) {
        canvas.drawText(HANDLEDETAILS1, 100.0f, 400.0f, paint);
        canvas.drawText("2、工作时间为周一、周三全天，周四下午。", 100.0f, 430.0f, paint);
        canvas.drawText("3、自体检日起7个有效工作日凭身份证领取健康证。", 100.0f, 460.0f, paint);
        canvas.drawText("4、此表一个月内有效,过期作废。", 100.0f, 490.0f, paint);
        canvas.drawText("5、体检步骤为:体征检查、胸透检查、血液检查、肛拭检查。", 100.0f, 520.0f, paint);
    }

    public static void canvasDrawText(Canvas canvas, String s, String string, Paint paint, String s6) {
        canvas.drawText(HANDLEDETAILS1, 100.0f, 400.0f, paint);
        canvas.drawText(HANDLEDETAILS2, 100.0f, 430.0f, paint);
        final StringBuilder sb = new StringBuilder();
        sb.append(HANDLEDETAILS3);
        sb.append(Contants.HOSPITALWEEK);
        sb.append(" ");
        string = (s = sb.toString());
        if (StringUtils.isNotEmpty(Contants.HOSPITALAM)) {
            final StringBuilder sb2 = new StringBuilder();
            sb2.append(string);
            sb2.append(Contants.HOSPITALAM);
            sb2.append(" ");
            s = sb2.toString();
        }
        string = s;
        if (StringUtils.isNotEmpty(Contants.HOSPITALPM)) {
            final StringBuilder sb3 = new StringBuilder();
            sb3.append(s);
            sb3.append(Contants.HOSPITALPM);
            string = sb3.toString();
        }
        final StringBuilder sb4 = new StringBuilder();
        sb4.append(string);
        sb4.append("法定假日除外。");
        canvas.drawText(sb4.toString(), 100.0f, 460.0f, paint);
        canvas.drawText(HANDLEDETAILS4, 100.0f, 490.0f, paint);
        canvas.drawText(HANDLEDETAILS5, 100.0f, 520.0f, paint);
        if (HOSPITALID == 112) { // 佛祖岭
            canvas.drawText("6、体检流程：①一楼“放射科“透视→②二楼面对“体检登记处“左边检查室（1）", 100.0f, 550.0f, paint);
            canvas.drawText("           做皮肤痢疾→③面对前台最左边化验室做甲肝、戊肝", 100.0f, 580.0f, paint);
            canvas.drawText(HANDLEDETAILS7 + s6, 100.0f, 610.0f, paint);
        } else if(HOSPITALID == 121) {
            canvas.drawText("6、体检步骤为：①内（外）科诊室-体征检查→②放射、胸透检查", 100.0f, 550.0f, paint);
            canvas.drawText("           →③导医台领试管→④检验科-血液检查", 100.0f, 580.0f, paint);
            canvas.drawText(HANDLEDETAILS7 + s6, 100.0f, 610.0f, paint);
        } else {
            canvas.drawText(HANDLEDETAILS6, 100.0f, 550.0f, paint);
            canvas.drawText(HANDLEDETAILS7 + s6, 100.0f, 580.0f, paint);
        }
    }

    // 调整照片旋转
    public static Bitmap adjustPhotoRotation(final Bitmap bitmap, final int n) {
        try {
            final int width = bitmap.getWidth();
            final int height = bitmap.getHeight();
            final Matrix matrix = new Matrix();
            matrix.setRotate((float) n, width / 2.0f, height / 2.0f);
            float n2 = 0.0f;
            float n3 = 0.0f;
            if (n == 90 || n == 270) {
                if (width > height) {
                    n2 = height / 2.0f - width / 2.0f;
                    n3 = 0.0f - n2;
                } else {
                    n3 = width / 2.0f - height / 2.0f;
                    n2 = 0.0f - n3;
                }
            }
            matrix.postTranslate(n2, n3);
            final Bitmap bitmap2 = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);
            new Canvas(bitmap2).drawBitmap(bitmap, matrix, new Paint());
            return bitmap2;
        } catch (Exception ex) {
            ex.printStackTrace();
            return bitmap;
        } catch (OutOfMemoryError outOfMemoryError) {
            outOfMemoryError.printStackTrace();
            return bitmap;
        }
    }

    public static Bitmap creatBarcode(String contents, int desiredWidth, int desiredHeight) {
        MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix result = null;
        try {
            result = writer.encode(contents, barcodeFormat, desiredWidth,
                    desiredHeight);
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int width = result.getWidth();
        int height = result.getHeight();
        int[] pixels = new int[width * height];
        // All are 0, or black, by default
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    public static Bitmap getBitmap(final String s) {
        try {
            return BitmapFactory.decodeStream(new URL(s).openStream());
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static Bitmap imageScale(final Bitmap bitmap, final int n, final int n2) {
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();
        final float n3 = n / width;
        final float n4 = n2 / height;
        final Matrix matrix = new Matrix();
        matrix.postScale(n3, n4);
        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }

    public static void saveBitmap(final Bitmap bitmap, final String s, final String s2) {
        final File file = new File(s);
        if (!file.exists()) {
            file.mkdirs();
        }
        final File file2 = new File(s, s2);
        if (file2.exists()) {
            file2.delete();
        }
        try {
            final FileOutputStream fileOutputStream = new FileOutputStream(file2);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, (OutputStream) fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException ex2) {
            ex2.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
